package com.cloudptt.api.product.operator;

import java.io.File;
import java.util.List;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.product.func.SipOperator;
import com.cloudptt.api.product.log.Log;
import com.mcptt.db.loader.msg.SessionListLoader;
import com.mcptt.db.loader.msg.SessionListLoaderCb;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.HistoryInfo;
import com.mcptt.db.model.SessionInfo;
import com.mcptt.db.op.HistoryDbOperator;
import com.mcptt.http.operator.IHttpOpCb;
import com.mcptt.http.operator.file.DownloadFileLoader;
import com.mcptt.http.operator.file.OnDownloadListener;
import com.mcptt.http.operator.file.OnUploadListener;
import com.mcptt.http.operator.file.UploadFileLoader;

import android.content.Context;
import android.os.RemoteException;

public class MsgOperator extends SipOperator 
{
	private final static String THIS_FILE = "MsgOperator";
//	private boolean isAuthorize_Data = false;
	
	private HistoryDbOperator dbOperater = null;
	public MsgOperator(Context ctx,HistoryDbOperator dbOperater)
	{
		super(ctx);
		this.dbOperater = dbOperater;
	}
	
//	public void onPublish(int result)
//	{
//		Log.d(THIS_FILE,  "onPublish result = " + result);
//		if((result & UserInfo.McType_Data)  == UserInfo.McType_Data)
//		{
//			isAuthorize_Data = true;
//		}
//		else
//		{
//			isAuthorize_Data = false;
//		}
//	}
	
	public List<HistoryInfo> getHistoryInfoList()
	{
		return dbOperater.getHistoryInfoList();
	}
//	public HirstoryListLoader getHirstoryListLoader(Context ctx,HirstoryListLoaderCb callback)
//	{
//		return new HirstoryListLoader(ctx,callback);
//	}
	
	public SessionListLoader getSessionListLoader(Context ctx,AddrInfo curUserInfo,SessionListLoaderCb callback)
	{
//		dbOperater.resetMsgCount(curUserInfo);
		return new SessionListLoader(ctx,curUserInfo,callback);
	}
	public boolean hasNewMsg()
	{
		return dbOperater.hasNewMsg();
	}
	
	public boolean delHirstory(boolean isGroup,String receiverUId)
	{
		return dbOperater.delHirstory(isGroup,receiverUId);
	}
	
	public boolean delSession(SessionInfo info)
	{
		return dbOperater.delSession(info);
	}
	
	public int sendMessage(int sessionType,String toNumber,SessionInfo msgInfo) throws RemoteException
	{
		Log.d(THIS_FILE, "sendMessage toNumber = " + toNumber + " msgInfo = " + msgInfo);
		int result = checkFirst();
		if(CauseCont.OperateCheck_Suc != result)
		{
			return result;
		}
//		if(!isAuthorize_Data)
//		{
//			return CauseCont.OperateErr_NoAuthorize_Data;
//		}
		String msgStr = msgInfo.getContent();
		result = service.sendMessage(sessionType,msgInfo.getMsgtype(),msgInfo.getLen(),msgStr, toNumber);
		if(result == CauseCont.OperateCheck_Suc) 
		{
			dbOperater.insertNewSession(msgInfo,true);
		}
		return result;
	}
	
	public void resetSessionCount(AddrInfo curUserInfo)
	{
		dbOperater.resetSessionCount(curUserInfo);
	}
	public void downloadFile(String svrIp,String jsonStr,IHttpOpCb callback,OnDownloadListener listener)
	{
		DownloadFileLoader loader = new DownloadFileLoader(svrIp,jsonStr,callback,listener);
		loader.downLoad();
	}
	public void uploadFile(final String url,File file,long len,final IHttpOpCb callback,OnUploadListener listener,String num)
	{
		UploadFileLoader loader = new UploadFileLoader(url,file,len,callback,listener,num);
		loader.upLoad();
	}

	public void notification()
	{
		Log.d(THIS_FILE, "notification");
		if(service == null)
		{
			Log.e(THIS_FILE, "service == null ");
			return;
		}
		try
		{
			service.ring(2,true);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}
